/*******************************************************************************
File to run regressions to look at measurement error

Summary: Loads anonymized data, runs regressions, outputs tables as doc files

Inputs: "data_forIPAregressions_anonymized.dta"

Outputs: "table3.doc"
		 "table4.doc"
		 "table5.doc"
		 "table6.doc"
		 "table7.doc"
		 "table8.doc"
		 "table9.doc"
		 "table10.doc"
		 "table11.doc"
		 "table12.doc"
		 "table13.doc"
		 "table14.doc"
	
Written: 4/14/2023	by Madeleine Walker (v1 of this dofile by Rachel Sayers)

*******************************************************************************/


/*************************************************
Analysis
*************************************************/
//load data
use "data_forIPAregressions_anonymized_v2.dta", clear

//generate maize productivity measure (standardized by area of plot)
gen maize_productivity_actualarea = maize_harvested_ifplanted_/(area/4046.86)
label var maize_productivity_actualarea "Reported Maize Productivity (kg/acre)"

//generate size difference outcome variable (PlotSize_ in sq m and area in acres)
gen size_diff = PlotSize_ - area*0.000247105
label var size_diff "Reported Size - Actual Size (sq. meters)"

//label plot distance measures
label var PlotDistRes_ "Reported Distance from Residence to Plot (Minutes)"
label var length "Actual Distance from Residence to Plot (m)"

//label ndvi
label var ndvi2 "Mean NDVI (June 16-Aug 31)"

//generate any ownership variable based on self-report
gen owner = 0
replace owner = 1 if I__T_171_1 <= 3 | I__T_171_1 == 9 | I__T_171_1 == 10
replace owner = . if I__T_171_1 == 7 | I__T_171_1 == 11 | I__T_171_1 >=98
label var owner "Owner"
gen sole_owner = 0
replace sole_owner = 1 if I__T_171_1 == 1
replace sole_owner = . if I__T_171_1 == 7 | I__T_171_1 == 11 | I__T_171_1 >=98
gen joint_owner = 0
replace joint_owner = 1 if owner == 1 & sole_owner == 0
replace joint_owner = . if I__T_171_1 == 7 | I__T_171_1 == 11 | I__T_171_1 >=98
gen no_owner = 0
replace no_owner = 1 if owner == 0
replace no_owner = . if I__T_171_1 == 7 | I__T_171_1 == 11 | I__T_171_1 >=98

//generate any decider variable based on self-report
gen decider = 0
replace decider = 1 if (inputs_decision == 1 | inputs_decision == 5 | inputs_decision == 6 | inputs_decision == 7 | inputs_decision == 10 | inputs_decision == 11) | (crop_decision == 1 | crop_decision == 5 | crop_decision == 6 | crop_decision == 7 | crop_decision == 10 | crop_decision == 11) | (harvest_decision == 1 | harvest_decision == 5 | harvest_decision == 6 | harvest_decision == 7 | harvest_decision == 10 | harvest_decision == 11)
replace decider = . if inputs_decision >=98 & crop_decision >=98 & harvest_decision >=98
label var decider "Decider"
gen sole_decider = 0
replace sole_decider = 1 if inputs_decision == 1 | crop_decision == 1 | harvest_decision == 1
replace sole_decider = . if inputs_decision >=98 & crop_decision >=98 & harvest_decision >=98
gen joint_decider = 0
replace joint_decider = 1 if decider == 1 & sole_decider == 0
replace joint_decider = . if inputs_decision >=98 & crop_decision >=98 & harvest_decision >=98
gen no_decider = 0
replace no_decider = 1 if decider == 0
replace no_decider = . if inputs_decision >=98 & crop_decision >=98 & harvest_decision >=98

//gen male variable based on gender
gen male = 0
replace male = 1 if gender == 1
label var male "Male"

//gen male*owner interaction variable
gen male_owner = male*owner
label var male_owner "Male*Owner"

//gen male*decider interaction variable
gen male_decider = male*decider
label var male_decider "Male*Decider"

//adjust and label WTP to keep income variable
//note: for women, this variable is originally their own WTP; for men the variable is their wife's WTP.
label var keep_pay "Women's Willingness to Pay to Keep Income"
gen keep_pay_spouse = keep_pay
label var keep_pay_spouse "Wife's Willingness to Pay to Keep Income"

//label measured bargaining power variable
label var pers_aut "Women's Measured Bargaining Power"
label var pers_aut_spouse "Wife's Measured Bargaining Power"

//generate and label owner/decider and WTP to keep income interactions
gen keep_pay_owner = keep_pay * owner
label var keep_pay_owner "Owner*Women's Willingness to Pay to Keep Income"
gen keep_pay_decider = keep_pay * decider
label var keep_pay_decider "Decider*Women's Willingness to Pay to Keep Income"
gen keep_pay_owner_spouse = keep_pay * owner
label var keep_pay_owner_spouse "Owner*Wife's Willingness to Pay to Keep Income"
gen keep_pay_decider_spouse = keep_pay * decider
label var keep_pay_decider_spouse "Decider*Wife's Willingness to Pay to Keep Income"

//generate and label owner/decider and measured bargaing power interactions
gen pers_aut_owner = pers_aut * owner
label var pers_aut_owner "Owner*Women's Measured Bargaining Power"
gen pers_aut_decider = pers_aut * decider
label var pers_aut_decider "Decider*Women's Measured Bargaining Power"
gen pers_aut_owner_spouse = pers_aut_spouse * owner
label var pers_aut_owner_spouse "Owner*Wife's Measured Bargaining Power"
gen pers_aut_decider_spouse = pers_aut_spouse * decider
label var pers_aut_decider_spouse "Decider*Wife's Measured Bargaining Power"

//label self-reported and spouse's empowerment indices and create interactions with owner/decider
local indices = "ao_index_i as_index_i df_index_i we_index_i be_index_i bi_index_i lr_index_i kr_index_i fm_index_i pg_index_i pc_index_i"
local ao_index_i_label = "Asset Ownership"
local as_index_i_label = "Participation in Selling Household Assets"
local df_index_i_label = "Household Decision-Making Power"
local we_index_i_label = "Employment Ability"
local be_index_i_label = "Beliefs about Female Leadership"
local bi_index_i_label = "Beliefs about Intimate Partner Violence"
local lr_index_i_label = "Knowledge of Legal Rights"
local kr_index_i_label = "Knowledge of Legal Resources"
local fm_index_i_label = "Freedom of Movement Inside and Outside Community"
local pg_index_i_label = "Participation in Community Groups"
local pc_index_i_label = "Participation in Community Activism"

foreach i of local indices {
	label var `i' "Women's ``i'_label'"
	gen `i'_owner = `i'*owner
	label var `i'_owner "Owner*Women's ``i'_label'" 
	gen `i'_decider = `i'*decider
	label var `i'_decider "Decider*Women's ``i'_label'"
	
	label var `i'_spouse "Wife's ``i'_label'"
	gen `i'_owner_spouse = `i'_spouse*owner
	label var `i'_owner_spouse "Owner*Wife's ``i'_label'"
	gen `i'_decider_spouse = `i'_spouse*decider
	label var `i'_decider_spouse "Decider*Wife's ``i'_label'"
}

//keep one observation per hhid*gender*plot (this elimates cases where duplicates due to one plot matching to multiple spouse plots)
gen tag = 0
gsort hhid gender plot -both_90 -one_90
bysort hhid gender plot: replace tag = 1 if _n == 1

keep if tag == 1
drop tag

//keep plots for which ranked straight-line distance from house to plot and ranked area of the plot walk path is the same as the ranked reported distance and reported area of the plot in the survey, respectively, and to plots for which actual and reported plot area is less than 20 acres and for which straight line distance from house to plot is less than 4 km and reported distance is less than 100 minutes
keep if subsample == 1

//table 3
reghdfe size_diff male, absorb(Village) cluster(hhid)
est store reg1
reghdfe size_diff male owner, absorb(Village) cluster(hhid)
est store reg2
reghdfe size_diff male owner male_owner, absorb(Village) cluster(hhid)
est store reg3
reghdfe size_diff male decider, absorb(Village) cluster(hhid)
est store reg4
reghdfe size_diff male decider male_decider, absorb(Village) cluster(hhid)
est store reg5

summ size_diff if male == 0
local womanmean = r(mean)

outreg2 [reg1 reg4 reg5 reg3] using "table3.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2

//tables 4-5
local count = 1
local indices = "df_index_i we_index_i be_index_i bi_index_i"
reghdfe size_diff owner if male == 0, absorb(Village) cluster(hhid)
est store regowner1
reghdfe size_diff decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider1
foreach i of local indices {
	local count = `count' + 1
	reghdfe size_diff owner `i' `i'_owner if male == 0, absorb(Village) cluster(hhid)
	est store regowner`count'
	reghdfe size_diff decider `i' `i'_decider if male == 0, absorb(Village) cluster(hhid)
	est store regdecider`count'
}
reghdfe size_diff owner keep_pay keep_pay_owner if male == 0, absorb(Village) cluster(hhid)
est store regowner6
reghdfe size_diff owner pers_aut pers_aut_owner if male == 0, absorb(Village) cluster(hhid)
est store regowner7
reghdfe size_diff decider keep_pay keep_pay_decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider6
reghdfe size_diff decider pers_aut pers_aut_decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider7

summ size_diff if male == 0
local womanmean = r(mean)

outreg2 [regowner1 regowner2 regowner3 regowner4 regowner5 regowner6 regowner7] using "table5.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2
outreg2 [regdecider1 regdecider2 regdecider3 regdecider4 regdecider5 regdecider6 regdecider7] using "table4.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2

//tables 6-7
local count = 1
local indices = "df_index_i we_index_i be_index_i bi_index_i"
reghdfe size_diff owner if male == 1, absorb(Village) cluster(hhid)
est store regowner1
reghdfe size_diff decider if male == 1, absorb(Village) cluster(hhid)
est store regdecider1
foreach i of local indices {
	local count = `count' + 1
	reghdfe size_diff owner `i'_spouse `i'_owner_spouse if male == 1, absorb(Village) cluster(hhid)
	est store regowner`count'
	reghdfe size_diff decider `i'_spouse `i'_decider_spouse if male == 1, absorb(Village) cluster(hhid)
	est store regdecider`count'
}
reghdfe size_diff owner keep_pay_spouse keep_pay_owner_spouse if male == 1, absorb(Village) cluster(hhid)
est store regowner6
reghdfe size_diff owner pers_aut_spouse pers_aut_owner_spouse if male == 1, absorb(Village) cluster(hhid)
est store regowner7
reghdfe size_diff decider keep_pay_spouse keep_pay_decider_spouse if male == 1, absorb(Village) cluster(hhid)
est store regdecider6
reghdfe size_diff decider pers_aut_spouse pers_aut_decider_spouse if male == 1, absorb(Village) cluster(hhid)
est store regdecider7

summ size_diff if male == 1
local manmean = r(mean)

outreg2 [regowner1 regowner2 regowner3 regowner4 regowner5 regowner6 regowner7] using "table7.doc", replace label nonote noni nocons addstat(Male sample mean, `manmean') nor2
outreg2 [regdecider1 regdecider2 regdecider3 regdecider4 regdecider5 regdecider6 regdecider7] using "table6.doc", replace label nonote noni nocons addstat(Male sample mean, `manmean') nor2

//table 8
reghdfe PlotDistRes_ length male, absorb(Village) cluster(hhid)
est store reg1
reghdfe PlotDistRes_ length male decider, absorb(Village) cluster(hhid)
est store reg2
reghdfe PlotDistRes_ length male decider male_decider, absorb(Village) cluster(hhid)
est store reg3
reghdfe PlotDistRes_ i.length_bin male, absorb(Village) cluster(hhid)
est store reg4
reghdfe PlotDistRes_ i.length_bin male decider male_decider, absorb(Village) cluster(hhid)
est store reg5

summ PlotDistRes_ if male == 0
local womanmean = r(mean)

outreg2 [reg1 reg2 reg3 reg4 reg5] using "table8.doc", replace label nonote noni nocons addstat(Women's mean, `womanmean') keep(PlotDistRes_ length male decider male_decider) nor2

//table 9
reghdfe PlotDistRes_ length male, absorb(Village) cluster(hhid)
est store reg1
reghdfe PlotDistRes_ length male owner, absorb(Village) cluster(hhid)
est store reg2
reghdfe PlotDistRes_ length male owner male_owner, absorb(Village) cluster(hhid)
est store reg3
reghdfe PlotDistRes_ i.length_bin male, absorb(Village) cluster(hhid)
est store reg4
reghdfe PlotDistRes_ i.length_bin male owner male_owner, absorb(Village) cluster(hhid)
est store reg5

summ PlotDistRes_ if male == 0
local womanmean = r(mean)

outreg2 [reg1 reg2 reg3 reg4 reg5] using "table9.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean')  keep(PlotDistRes_ length male owner male_owner) nor2

//tables 10-11
local count = 1
local indices = "df_index_i we_index_i be_index_i bi_index_i"
reghdfe PlotDistRes_ length owner if male == 0, absorb(Village) cluster(hhid)
est store regowner1
reghdfe PlotDistRes_ length decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider1
foreach i of local indices {
	local count = `count' + 1
	reghdfe PlotDistRes_ length owner `i' `i'_owner if male == 0, absorb(Village) cluster(hhid)
	est store regowner`count'
	reghdfe PlotDistRes_ length decider `i' `i'_decider if male == 0, absorb(Village) cluster(hhid)
	est store regdecider`count'
}
reghdfe PlotDistRes_ length owner keep_pay keep_pay_owner if male == 0, absorb(Village) cluster(hhid)
est store regowner6
reghdfe PlotDistRes_ length owner pers_aut pers_aut_owner if male == 0, absorb(Village) cluster(hhid)
est store regowner7
reghdfe PlotDistRes_ length decider keep_pay keep_pay_decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider6
reghdfe PlotDistRes_ length decider pers_aut pers_aut_decider if male == 0, absorb(Village) cluster(hhid)
est store regdecider7

summ PlotDistRes_ if male == 0
local womanmean = r(mean)

outreg2 [regowner1 regowner2 regowner3 regowner4 regowner5 regowner6 regowner7] using "table11.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2
outreg2 [regdecider1 regdecider2 regdecider3 regdecider4 regdecider5 regdecider6 regdecider7] using "table10.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2

//tables 12-13
local count = 1
local indices = "df_index_i we_index_i be_index_i bi_index_i"
reghdfe PlotDistRes_ length owner if male == 1, absorb(Village) cluster(hhid)
est store regowner1
reghdfe PlotDistRes_ length decider if male == 1, absorb(Village) cluster(hhid)
est store regdecider1
foreach i of local indices {
	local count = `count' +1
	reghdfe PlotDistRes_ length owner `i'_spouse `i'_owner_spouse if male == 1, absorb(Village) cluster(hhid)
	est store regowner`count'
	reghdfe PlotDistRes_ length decider `i'_spouse `i'_decider_spouse if male == 1, absorb(Village) cluster(hhid)
	est store regdecider`count'
}
reghdfe PlotDistRes_ length owner keep_pay_spouse keep_pay_owner_spouse if male == 1, absorb(Village) cluster(hhid)
est store regowner6
reghdfe PlotDistRes_ length owner pers_aut_spouse pers_aut_owner_spouse if male == 1, absorb(Village) cluster(hhid)
est store regowner7
reghdfe PlotDistRes_ length decider keep_pay_spouse keep_pay_decider_spouse if male == 1, absorb(Village) cluster(hhid)
est store regdecider6
reghdfe PlotDistRes_ length decider pers_aut_spouse pers_aut_decider_spouse if male == 1, absorb(Village) cluster(hhid)
est store regdecider7

summ PlotDistRes_ if male == 1
local manmean = r(mean)

outreg2 [regowner1 regowner2 regowner3 regowner4 regowner5 regowner6 regowner7] using "table13.doc", replace label nonote noni nocons addstat(Male sample mean, `manmean') nor2
outreg2 [regdecider1 regdecider2 regdecider3 regdecider4 regdecider5 regdecider6 regdecider7] using "table12.doc", replace label nonote noni nocons addstat(Male sample mean, `manmean') nor2

//table 14
reghdfe maize_productivity_actualarea ndvi2 male, absorb(Village) cluster(hhid)
est store reg1
reghdfe maize_productivity_actualarea ndvi2 male owner, absorb(Village) cluster(hhid)
est store reg2
reghdfe maize_productivity_actualarea ndvi2 male owner male_owner, absorb(Village) cluster(hhid)
est store reg3
reghdfe maize_productivity_actualarea ndvi2 male decider `ifstatement', absorb(Village) cluster(hhid)
est store reg4
reghdfe maize_productivity_actualarea ndvi2 male decider male_decider `ifstatement', absorb(Village) cluster(hhid)
est store reg5

summ maize_productivity_actualarea if male == 0
local womanmean = r(mean)

outreg2 [reg1 reg4 reg5 reg3] using "table14.doc", replace label nonote noni nocons addstat(Female sample mean, `womanmean') nor2
